#include<iostream>
using namespace std;

#include "menu.h"
#include "Gotoxy.h"

Menu::Menu(MenuType which_menu, bool withHiding)
{
	switch(which_menu)
	{
		case MAIN:
			m_options_text.push_back("Add shape");
			if(withHiding)
				m_options_text.push_back("Draw (with hiding)");
			else
				m_options_text.push_back("Draw (with cuts)");
			if(withHiding)
				m_options_text.push_back("Change default drawing option to Draw With Cuts");
			else
				m_options_text.push_back("Change default drawing option to Draw With Hiding");
			m_options_text.push_back("Choose a Shape");
			m_options_text.push_back("Show Animation With Screen Transfer");
			m_options_text.push_back("Show Animation With Collisions");
			m_options_text.push_back("Save to file");
			m_options_text.push_back("Load from file");
			m_options_text.push_back("Exit");
			break;
		case CHOOSE_SHAPE:
			m_options_text.push_back("Cancel Selection");
			m_options_text.push_back("Delete Shape");
			m_options_text.push_back("Bring To Front");
			m_options_text.push_back("Move To Back");
			m_options_text.push_back("Shift the Shape");
			m_options_text.push_back("Add Animation");
			break;
		case ADD_SHAPE:
			m_options_text.push_back("Add Rectangle");
			m_options_text.push_back("Add Circle");
			break;
		case SAVE:
			m_options_text.push_back("Save as text");
			m_options_text.push_back("Save as binary");
			break;
		case LOAD:
			m_options_text.push_back("Load from text");
			m_options_text.push_back("Load from binary");
			break;
	}
}

void Menu::displayOptions()//displayes the options of a given menu to the user.
{
	list<string>::iterator itr;
	int i;
	for ( i=1, itr = m_options_text.begin(); itr != m_options_text.end(); i++,++itr)
		cout << i << "> " << *itr <<endl;
	cout << "Please enter your choice: ";
}

unsigned int Menu::choose()//provides the user the interface to choose an action from a given menu.
{
	unsigned int m_num_of_options = m_options_text.size();
	unsigned int l_choise = m_num_of_options;

	clrscr();
	displayOptions();
	cin >> l_choise;

	while( l_choise >= m_num_of_options && l_choise <= 0)
	{
		cerr << "Wrong Option! Please choose an option between 1 and " << m_num_of_options << endl << endl;
		displayOptions();
		cin >> l_choise;
	}
	return l_choise;
}

void Menu::changeOption(int option_num, const string &option_text)//changes an option in given menu.
{
	list<string>::iterator itr = m_options_text.begin();
	int i=1;
	while( i < option_num)
	{
		i++;
		++itr;
	}
	*itr = option_text;
}